//
//  SZYingKUMainViewController.m
//  KaiJing
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "SZYingKUMainViewController.h"
#import <MJExtension.h>
#import "UMSocial.h"
#import "HYMovieMainView.h"
#import "PKRequestManager.h"
#import "HYMovieDateModel.h"
#import "HYMainMoviePlagerViewController.h"
#import "HYMovieDateTableViewController.h"
#import "CJPictorialCellWebViewViewController.h"
#import "HYMovieMoreViewController.h"
#import "SZBaseNavController.h"
@interface SZYingKUMainViewController ()<UIScrollViewDelegate,bigImageButton>

@property (nonatomic, strong) HYMovieMainView *bigView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation SZYingKUMainViewController


- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.navigationController.navigationBar.translucent = NO;
    [self setUpData];
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"更多" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

#pragma mark -- 解析数据--
- (void)setUpData
{
    NSUUID *uuid = [UIDevice currentDevice].identifierForVendor;
    NSString *uuidString = uuid.UUIDString;
    NSString *string = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=allthreadtovisit&orderbynew=1&androidflag=1&appfrom=ios&iosversion=3.13&page=10&deviceid=%@",uuidString];
    [PKRequestManager requestWithUrlString:string parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        
       NSArray *listArray = dataDic[@"data"][@"list"];
        _dataArray = [NSMutableArray array];
        
        for (NSDictionary *dic in listArray) {
            _model = [[HYYingKuMainModel alloc] init];
            [_model setValuesForKeysWithDictionary:dic];
            
            [_dataArray addObject:_model];
            
        }
        
       
    [self addView]; 
      
    } error:^(NSError *error) {
        
    }];

}

#pragma mark --初始化uivew
- (void)addView {
    
    _bigView = [[HYMovieMainView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _bigView.array = [NSMutableArray array];
    _bigView.array = _dataArray;
    self.view = _bigView;
     _bigView.bottomView.praiseButton.tag = 1001;
    [_bigView.bottomView.praiseButton addTarget:self action:@selector(praiseButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [_bigView.bottomView.moreButton addTarget:self action:@selector(moreButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    _bigView.scrollView.delegate = self;
    
    _bigView.delegate = self;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [_bigView addGestureRecognizer:tap];
    
    HYYingKuMainModel *model = _dataArray[0];
    self.navigationItem.title = model.subject;
   }


#pragma mark --跳转更多界面
- (void)rightButtonAction:(UIBarItem *)button {
    HYMovieMoreViewController *moreVc = [[HYMovieMoreViewController alloc] init];
    [self.navigationController pushViewController:moreVc animated:YES];
}

/** 点击大的image的图片触发方法*/
- (void)changeBigImageButton:(UIButton *)button

{
    HYMainMoviePlagerViewController *mainMoviePlayerVc = [[HYMainMoviePlagerViewController alloc] init];
    mainMoviePlayerVc.playerArray = _dataArray;
    mainMoviePlayerVc.playerModel = _dataArray[button.tag - 1000];
    mainMoviePlayerVc.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:mainMoviePlayerVc animated:NO];

}

#pragma mark --滑动触发方法--
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSUInteger content = scrollView.contentOffset.y / (kScreenHeight - 113);
    SZLog(@"%ld",content);
   // float content = _bigView.scrollView.contentOffset.y / kScreenHeight;
        for (int i  =1 ; i <= content; i ++) {
        _model = _dataArray[i];
        self.navigationItem.title = _model.subject;
            
    }
    if (content < 1) {
        _model = _dataArray[0];
        self.navigationItem.title = _model.subject;
    }
   
}

#pragma mark --轻拍手势触发方法--
-(void)tapAction:(UITapGestureRecognizer *)tap {
    
    CJPictorialCellWebViewViewController *YKWebView = [[CJPictorialCellWebViewViewController alloc] init];
    NSUInteger content = _bigView.scrollView.contentOffset.y / (kScreenHeight - 113);
    HYYingKuMainModel *model = _dataArray[content];
    YKWebView.tid = model.tid;
    [self.navigationController pushViewController:YKWebView animated:YES];
    
}

//点赞
/*
- (void)praiseButton:(UIButton *)button {
    button = _dataArray[button.tag - 1001];
    
    // 是否登录userName isEqualToString:@"YES"]
    if ([SZLoginTool shareLogin].isLogin) {
        
        button.selected = !button.selected;
        
        NSString *isLike = nil;
        if (button.selected) {
            isLike = @"like";
        } else {
            isLike = @"unlike";
        }
        
        NSString *str = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=threadlikecp&op=%@&tid=%@&formhash=%@&androidflag=1&appfrom=ios&iosversion=3.13",isLike,_model.tid, [SZLoginTool shareLogin].formhash];
        
        [PKRequestManager requestWithUrlString:str parDic:nil method:GET finish:^(NSDictionary *dataDic) {
            if (dataDic[@"data"][@"result"]) {
                SZLog(@"点赞成功");
                
                _model.isliked = button.selected;
            } else {
                SZLog(@"点赞失败");
            }
            
            
        } error:^(NSError *error) {
            
        }];
        
    } else {
        
        SZLoginViewController *loginVC = [[SZLoginViewController alloc] init];
        SZBaseNavController *loginNav = [[SZBaseNavController alloc] initWithRootViewController:loginVC];
        [self presentViewController:loginNav animated:YES completion:nil];
        
        
    }
}
*/

//- (void)praiseButton:(UIButton *)button {
//    NSLog(@"dafj");
//}

//分享
- (void)moreButton:(UIButton *)button {
    
    //    NSArray *listArray = self.model.list;
    //    CJShadowMovieListModel *model = listArray[button.tag - 12000];
    
    NSString *url = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=viewthread&tid=%@&isfrommobile=1",_model.tid];
    
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIViewController *viewController = window.rootViewController;
    [UMSocialSnsService presentSnsIconSheetView:viewController
                                         appKey:@"569ee21fe0f55a76b10021e3"
                                      shareText:[NSString stringWithFormat:@"<<%@>>详情点击%@",_model.subject,url]
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToEmail,UMShareToWechatFavorite,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone,UMShareToTencent,nil]
                                       delegate:nil];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
